package com.tengniu.web.controller.buss;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.buss.domain.MassageChairPackage;
import com.tengniu.buss.service.MassageChairPackageService;
import com.tengniu.common.annotation.Log;
import com.tengniu.common.buss.enums.YesOrNoEnum;
import com.tengniu.common.core.controller.BaseController;
import com.tengniu.common.core.domain.AjaxResult;
import com.tengniu.common.core.page.TableDataInfo;
import com.tengniu.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 按摩椅套餐管理
 */
@RestController
@RequestMapping("/admin/package")
@Slf4j
public class PackageAdminController extends BaseController {

    @Resource
    private MassageChairPackageService messageChairPackageService;

    /**
     * 获取按摩椅套餐
     */
    @PreAuthorize("@ss.hasPermi('buss:package:list')")
    @PostMapping("/list")
    public TableDataInfo<MassageChairPackage> list(@RequestBody MassageChairPackage massageChairPackage) {
        startPage();
        List<MassageChairPackage> list = messageChairPackageService.list(
                Wrappers.lambdaQuery(MassageChairPackage.class)
                        .eq(Objects.nonNull(massageChairPackage.getStatus()), MassageChairPackage::getStatus, massageChairPackage.getStatus())
                        .like(Objects.nonNull(massageChairPackage.getPackageName()), MassageChairPackage::getPackageName, massageChairPackage.getPackageName()));
        return getDataTable(list);
    }

    /**
     * 新增按摩椅套餐
     */
    @PreAuthorize("@ss.hasPermi('buss:package:add')")
    @Log(title = "按摩椅套餐", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MassageChairPackage massageChairPackage) {
        return toAjax(messageChairPackageService.save(massageChairPackage));
    }

    /**
     * 修改按摩椅套餐
     */
    @PreAuthorize("@ss.hasPermi('buss:package:edit')")
    @Log(title = "按摩椅套餐", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MassageChairPackage massageChairPackage) {
        return toAjax(messageChairPackageService.updateById(massageChairPackage));
    }

    /**
     * 删除按摩椅套餐
     */
    @PreAuthorize("@ss.hasPermi('buss:package:remove')")
    @Log(title = "按摩椅套餐", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{bannerId}")
    public AjaxResult remove(@PathVariable Long bannerId) {
        return toAjax(messageChairPackageService.removeById(bannerId));
    }

    /**
     * 上架
     */
    @PreAuthorize("@ss.hasPermi('buss:package:shelf')")
    @Log(title = "按摩椅套餐", businessType = BusinessType.UPDATE)
    @GetMapping("/shelf/{massageChairPackageId}")
    public AjaxResult shelf(@PathVariable Long massageChairPackageId) {
        MassageChairPackage massageChairPackage = messageChairPackageService.getById(massageChairPackageId);
        if (massageChairPackage == null) {
            return AjaxResult.error("按摩椅套餐不存在");
        }
        massageChairPackage.setStatus(YesOrNoEnum.YES.getCode());
        return toAjax(messageChairPackageService.updateById(massageChairPackage));
    }

    /**
     * 下架
     */
    @PreAuthorize("@ss.hasPermi('buss:package:offShelf')")
    @Log(title = "按摩椅套餐", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelf/{massageChairPackageId}")
    public AjaxResult offShelf(@PathVariable Long massageChairPackageId) {
        MassageChairPackage massageChairPackage = messageChairPackageService.getById(massageChairPackageId);
        if (massageChairPackage == null) {
            return AjaxResult.error("按摩椅套餐不存在");
        }
        massageChairPackage.setStatus(YesOrNoEnum.NO.getCode());
        return toAjax(messageChairPackageService.updateById(massageChairPackage));
    }

}
